package com.leostadyn.stackfarm.timestatistics.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.leostadyn.stackfarm.timestatistics.R;
import com.leostadyn.stackfarm.timestatistics.model.bean.EmployeeBean;

import java.util.List;

public class MainPersonnelAdapter extends RecyclerView.Adapter<MainPersonnelAdapter.VH> {
    private List<EmployeeBean> list;
    private OnRecyclerItemClickListener mItemClickListener;

    public MainPersonnelAdapter(List<EmployeeBean> list) {
        this.list = list;
    }

    public void setRecyclerItemClickListener(OnRecyclerItemClickListener listener) {
        mItemClickListener = listener;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_admin_main_personnel, viewGroup, false);
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VH viewHolder, int i) {

        viewHolder.nameTxt.setText(list.get(i).getName());
        viewHolder.jobTxt.setText(list.get(i).getPostName());
        viewHolder.dayTxt.setText(list.get(i).getWorkingHours() + "天");
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public interface OnRecyclerItemClickListener {
        //RecyclerView的点击事件，将信息回调给view
        void onItemClick(int Position, List<EmployeeBean> dataBeanList);
    }

    class VH extends RecyclerView.ViewHolder {
        TextView nameTxt;
        TextView jobTxt;
        TextView dayTxt;

        VH(@NonNull View itemView) {
            super(itemView);
            nameTxt = itemView.findViewById(R.id.txt_personnel_name);
            jobTxt = itemView.findViewById(R.id.txt_personnel_job);
            dayTxt = itemView.findViewById(R.id.txt_personnel_day);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemClickListener != null)
                        mItemClickListener.onItemClick(getAdapterPosition(), list);
                }
            });
        }

    }
}
